package org.openkanban.client.ui.footer;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.AnimationAcceleration;
import com.smartgwt.client.types.BkgndRepeat;
import com.smartgwt.client.widgets.AnimationCallback;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;

/**
 * GUI Element of the FooterPanel.
 */
public class FooterPanel extends HLayout {

    private boolean opened = false;

    private Footer footer;

    public FooterPanel(Footer footer) {
	this.footer = footer;

	Img gripImg = new Img("grip.png");
	gripImg.setWidth(30);
	gripImg.setHeight(18);

	setHeight(20);
	setAlign(Alignment.CENTER);
	setBackgroundImage("admin_panel_bg.png");
	setBackgroundRepeat(BkgndRepeat.REPEAT_X);
	addClickHandler(footerPanelClickHandler());
	addMember(gripImg);
    }

    private ClickHandler footerPanelClickHandler() {
	return new ClickHandler() {
	    @Override
	    public void onClick(ClickEvent event) {
		switchFooterOpenClose();
	    }
	};
    }

    /**
     * Can be called from outside: RootPanel
     */
    public void openFooter(AnimationCallback animationCallback) {
	if (!opened) {
	    opened = !opened;
	    setBackgroundImage("footer_panel.png");
	    footer.getFooterLayout().show();
	    footer.animateMove(0, footer.getTopPosition(), animationCallback, 666, AnimationAcceleration.SMOOTH_START_END);
	}
    }

    public void closeFooter(final AnimationCallback animationCallback) {
	opened = !opened;
	setBackgroundImage("admin_panel_bg.png");
	footer.animateMove(0, footer.getTopPosition(), new AnimationCallback() {
	    @Override
	    public void execute(boolean earlyFinish) {
		footer.getFooterLayout().hide();
		animationCallback.execute(earlyFinish);
	    }
	}, 666, AnimationAcceleration.SMOOTH_START_END);
    }

    private void switchFooterOpenClose() {
	if (opened) {
	    closeFooter(null);
	} else {
	    openFooter(null);
	}
    }

    public boolean isOpened() {
	return opened;
    }

}
